package de.le.mfw.client.main.view;

import com.google.gwt.user.client.ui.Widget;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Side;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.Layout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.events.HasTabSelectedHandlers;

import de.le.mfw.client.main.presenter.MainPresenter.MainView;
import de.le.mfw.client.main.view.widget.TabSetWithPermission;
import de.le.mfw.client.main.view.widget.TabSetWithPermission.Permission;
import de.le.mfw.client.util.IconPool;

public class MainViewImpl implements MainView {

	private HLayout subHeader = new HLayout();
	private TabSetWithPermission menu = new TabSetWithPermission();

	private Tab settingsTab = new Tab("Einstellungen",
			IconPool.Settings_Small.getUrl());

	public Widget asWidget() {
		VLayout main = new VLayout();
		main.setSize("100%", "100%");
		main.setMembersMargin(20);
		main.setLayoutMargin(5);

		HLayout header = new HLayout();
		header.setHeight(75);
		header.setWidth100();

		HLayout logo = new HLayout();
		logo.setWidth(200);
		logo.addMember(IconPool.Logo.createImage());
		header.addMember(logo);

		subHeader.setWidth("*");
		subHeader.setAlign(Alignment.RIGHT);
		subHeader.setMembersMargin(5);
		subHeader.setLayoutMargin(10);
		header.addMember(subHeader);

		main.addMember(header);

		menu.setTabBarPosition(Side.TOP);
		// menu.setWidth("*");
		menu.setWidth100();
		menu.setHeight("*");

		Tab homeTab = new Tab("Home", IconPool.Home_Small.getUrl());
		menu.addTab(homeTab);

		menu.addTab(settingsTab, Permission.LoggedIn);

		main.addMember(menu);

		return main;
	}

	public void setUserWidget(Widget userWidget) {
		subHeader.addMember(userWidget);
	}

	public void setUserRole(Permission userRole) {
		menu.setUserRole(userRole);
	}

	public void setContent(Widget content) {
		Layout container = new VLayout();
		container.setSize("100%", "100%");
		container.addMember(content);
		// menu.updateTab(menu.getSelectedTab(), container);

		menu.getSelectedTab().setPane(container);
	}

	public HasTabSelectedHandlers getMenu() {
		return menu;
	}

	public Tab getSettingsTab() {
		return settingsTab;
	}
}
